package com.haierp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.mapper.FindItemMapper;
import com.haierp.model.FindItem;
import com.haierp.model.FindItemSku;
import com.haierp.model.Inventory;
import com.haierp.model.Item;
import com.haierp.model.ItemSku;
import com.haierp.service.IFindItemService;
import com.haierp.service.IFindItemSkuService;
import com.haierp.service.IInventoryService;
import com.haierp.service.IItemService;
import com.haierp.service.IItemSkuService;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.ItemQueryVO;
import com.wangzhixuan.commons.utils.BeanUtils;
@Service("findItemService")
public class FindItemServiceImpl extends SuperServiceImpl<FindItemMapper, FindItem>implements IFindItemService {
    protected Logger logger = LogManager.getLogger(getClass());
    @Autowired
	private IFindItemService findItemService;
    @Autowired
	private IItemSkuService itemSkuService;
	
	@Autowired
	private IItemService itemService;
	
	@Autowired
	private IFindItemSkuService findItemSkuService;
	
	@Autowired
	private IInventoryService inventoryService;
	@Override
	public JsonPageResult<List<FindItem>> queryFindItems(ItemQueryVO itemQueryVO) {
		JsonPageResult<List<FindItem>> itemResult = new JsonPageResult<>();
		//1、查询总的记录数量
		Integer totalCount =  this.baseMapper.queryItemsCount(itemQueryVO);
		
		//2、查询分页记录
		if(totalCount!=null&&totalCount!=0){
			itemResult.buildPage(totalCount, itemQueryVO);
			List<FindItem> items = this.baseMapper.queryFindItems(itemQueryVO);
			itemResult.setData(items);
		}else{
			List<FindItem> items  = new ArrayList<>();
			itemResult.setData(items);
		}
		return itemResult;
	}

	@Override
	public JsonResult<String> passOrRefuse(FindItem item) {
		JsonResult<String> result = new JsonResult<>();
		FindItem findItem = findItemService.selectById(item.getId());
		List<FindItemSku> findItemSkus = findItemSkuService.queryFindItemSkusByItemId(item.getId());
		Item insertItem = new Item();
		BeanUtils.copyProperties(findItem, insertItem);
		Item itemTemp = new Item();
		itemTemp.setItemCode(insertItem.getItemCode());
		Item one = itemService.selectOne(itemTemp);
		if(one != null) {
			return result.buildIsSuccess(false).buildMsg("发现的商品已经存在");
		}
		itemService.insertSelective(insertItem);
		for (FindItemSku findItemSku : findItemSkus) {
			ItemSku itemSku = new ItemSku();
			BeanUtils.copyProperties(findItemSku, itemSku);
			itemSku.setItemId(insertItem.getId());
			itemSkuService.insert(itemSku);
		}
		ItemQueryVO itemQueryVO = new ItemQueryVO();
		itemQueryVO.setItemId(insertItem.getId());
		List<ItemSku> newItemSku = itemSkuService.queryItemSkusByItemId(itemQueryVO);
		for (int i = 0; i < newItemSku.size(); i++) {
			newItemSku.get(i).setVirtualInv((findItemSkus.get(i).getVirtualInv()));
		}
		List<Inventory> inventoryList = itemSkuService.initInventory(newItemSku);
		inventoryService.insertBatch(inventoryList);
		findItem.setPurchaseStatus(1);
		findItemService.insertOrUpdate(findItem);
		return result.buildIsSuccess(true);
	}


	

	
}
